package com.aizhixin.lab.quartz.job;

import com.aizhixin.lab.company.controller.WebSocketServer;
import com.aizhixin.lab.company.domain.SignData;
import com.aizhixin.lab.course.course.service.ReleaseSignInService;
import com.aizhixin.lab.quartz.entity.SignInfo;
import com.aizhixin.lab.quartz.repository.SignInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@Slf4j
public class ReleaseSignJob implements Job {
    private String parameter;
    @Autowired
    private ReleaseSignInService signInService;
    @Autowired
    private SignInfoRepository signInfoRepository;
    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        this.parameter= (String) jobExecutionContext.getMergedJobDataMap().get("parameter");
        SignInfo one = signInfoRepository.findOne(Long.parseLong(parameter));

        SignData signData = signInService.releaseSign(one.getCourseClassId(),one.getEndDate(),one.getCreateBy(),one.getType());

        List<String> ids = signData.getIds();
        for (String id:ids){
            WebSocketServer webSocketServer = WebSocketServer.getWebSocketSet().get(id);
            if (webSocketServer != null) {
//                    webSocketServer.sendMessage("你是谁");
                webSocketServer.getSession().getAsyncRemote().sendText("老师提醒你签到了,"+signData.getSignInCode());

            }
        }
        System.out.println(signData);
    }
}
